package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.service.ILoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.domain.Card;
import com.ktgj.customer.service.ICardService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 卡Controller
 *
 * @author ruoyi
 * @date 2021-07-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/card")
public class CardController extends BaseController {
    @Autowired
    private ICardService cardService;
    @Autowired
    private ILoginService loginService;

    static final Logger log = LoggerFactory.getLogger(CardController.class);

    /**
     * 查询卡列表
     */
    //@PreAuthorize(hasPermi = "customer:card:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Card card) {
        startPage();
        List<Card> list = cardService.selectCardList(card);
        return getDataTable(list);
    }

    /**
     * 导出卡列表
     */
    //@PreAuthorize(hasPermi = "customer:card:export")
    @Log(title = "卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Card card) throws IOException {
        List<Card> list = cardService.selectCardList(card);
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        util.exportExcel(response, list, "卡数据");
    }

    /**
     * 获取卡详细信息
     */
    //@PreAuthorize(hasPermi = "customer:card:query")
    @GetMapping(value = "/{cardId}")
    public AjaxResult getInfo(@PathVariable("cardId") Long cardId) {
        return AjaxResult.success(cardService.selectCardById(cardId));
    }

    /**
     * 获取卡详细信息
     */
    @PostMapping("/cardInfo")
    public Card cardInfo(@RequestBody Map<String, Object> map) {
        if (map.get("cardId") != null && !map.get("cardId").equals("")){
            Long cardId = Long.valueOf(map.get("cardId").toString());
            Card card = cardService.selectCardById(cardId);
            return card;
        }
        else {
            return null;
        }
    }

    /**
     * 新增卡
     */
    //@PreAuthorize(hasPermi = "customer:card:add")
    @Log(title = "卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Card card, HttpServletRequest request) {
        Employee employee = loginService.getLoginEmployee(request);
        return toAjax(cardService.insertCard(card, employee));
    }

    /**
     * 修改卡
     */
    //@PreAuthorize(hasPermi = "customer:card:edit")
    @Log(title = "卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Card card) {
        return toAjax(cardService.updateCard(card));
    }

    /**
     * 删除卡
     */
    //@PreAuthorize(hasPermi = "customer:card:remove")
    @Log(title = "卡", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cardIds}")
    public AjaxResult remove(@PathVariable Long[] cardIds) {
        return toAjax(cardService.deleteCardByIds(cardIds));
    }

    /**
     * 购卡页面会员卡及卡权益详情信息
     */
    @PostMapping("queryPurchaseCardAndRightsDetail")
    public AjaxResult queryPurchaseCardAndRightsDetail(HttpServletRequest request) {
        return AjaxResult.success(cardService.queryPurchaseCardAndRightsDetail(request));
    }
    
}
